<?php

namespace app\admin\controller;

use think\Db;
use think\Request;

class Banner extends Base
{
    public function __construct()
    {
        parent::__construct();
//        $this->isLogin();
    }

    /**
     * @route('banner/bannerlist')
     * 轮播图列表
     */
    public function bannerlist()
    {
        $limit = 10;
        $banner = Db::table('ddt_banner')->order('sort')->paginate($limit);
        $btype = [
          '1' => '首页轮播图',
          '2' => '家政轮播图',
          '3' => '新品轮播图',
        ];
        $page = $banner->render();
        if(!empty($banner)){
            $banners = $banner->toArray()['data'];
            foreach($banners as $key => $value){
                $banners[$key]['typename'] = $btype[$value['pos']];
            }
        }

//        var_dump($banner);die;
        $tvar = array(
            "title" => "轮播图列表",
            "time"  => time(),
            "data"  => $banners,
        );
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('banner/bannerlist');
    }


    /**
     * @route('banner/boxbanner')
     * 编辑添加轮播图
     */
    public function boxbanner(Request $request)
    {
        if(\request()->isAjax()){
            $param = $request->param();
            $data = [
                'title' => $param['title'],
                'pic_url' => $param['images'],
                'to_url' => $param['tourl'],
                'pos' => $param['pos'],
                'sort' => $param['sort'],
            ];
//            var_dump($param);die;
            // 处理
            if($param['id'] > 0){
                $rs = Db::table('ddt_banner')->where('id',$param['id'])->update($data);
            } else {
                $rs = Db::table('ddt_banner')->insert($data);
            }
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('操作成功','/admin/adminlist');
        }
        $id = $_GET['id'];
        $banner = null;
        if($id != 0){
            $banner = Db::table('ddt_banner')->where('id',$id)->find();
        }
        $tvar = array(
            "title" => "添加轮播图",
            "time"  => time(),
            "data" => $banner,
            "get" => $id,
        );
//        var_dump($tvar);die;
        $this->assign("data", $tvar);
        return $this->fetch('banner/boxbanner');
    }

    protected function json($msg){
        $msg = mb_convert_encoding( $msg, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
        return $msg;
    }

    /**
     * @route('banner/delbanner')
     * 删除轮播图
     */
    public function delbanner()
    {
        $id = $_GET['id'];
//        var_dump($id);die;
        $rs = Db::table('ddt_banner')->where('id',$id)->delete();
        if($rs === false){
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }

    /**
     * 轮播图上传
     * @route('banner/upload')
     */
    public function bannerunpload()
    {
        $res = \Upload::FileUpload('file','banner',1024*1024*10);
        if(!$res['data']){
            $this->result([],-1,$res['msg']);
        }
        $this->result($res['data']['file_path'],1);
    }

}